package com.dao; 
import com.model.${table_name}Model; 
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import com.common.DBUtilsHelper;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class ${table_name}DAO  {
	DBUtilsHelper dbh = new DBUtilsHelper();
	QueryRunner runner = dbh.getRunner();

    public List<${table_name}Model> selectAll()  {
		List<${table_name}Model> beanListResult = null;
		try {
			beanListResult = runner.query("select * from ${table_name}", new BeanListHandler<${table_name}Model>(${table_name}Model.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		return beanListResult;
    }
    
    <#if model_pri?exists>
    public int delete(${table_name}Model model)  {
        int i=0;
		try {
		    String sql="delete from ${table_name} where <#list model_pri as model>${model}=?<#if model_has_next>,</#if></#list>";			
			Object[] params = new Object[] {<#list model_pri as model>model.get${model}()<#if model_has_next>,</#if></#list>};
			i = runner.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		return i;
    }
    </#if>
   
    <#if model_npri?exists>
    public int update(${table_name}Model model)  {
        int i=0;
		try {
		    String sql="update ${table_name} set <#list model_npri as model>${model}=?<#if model_has_next>,</#if></#list> "+
		    " where <#list model_pri as model>${model}=?<#if model_has_next>,</#if></#list>";			
			Object[] params = new Object[] {<#list model_npri as model>model.get${model}()<#if model_has_next>,</#if></#list>,
			<#list model_pri as model>model.get${model}()<#if model_has_next>,</#if></#list>};
			i = runner.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
		return i;
    }
    </#if>
    
    <#if model_column?exists>
	public int add(${table_name}Model model) {
	    int i=0;
		try {
			String sql="insert into ${table_name}(<#list model_column as model>${model.changeColumnName}<#if model_has_next>,</#if> </#list>)"+
			  " values(<#list model_column as model>?<#if model_has_next>,</#if> </#list>)";
			Object[] params = new Object[] {<#list model_column as model>model.get${model.changeColumnName}()<#if model_has_next>,</#if> </#list>};
			i = runner.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}	
    </#if>
 }